package org.bluedream.core.module.service.stock;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.core.module.base.OrgBaseServiceImpl;
import org.bluedream.core.module.dao.stock.TmpSkuStockDao;
import org.bluedream.core.module.entity.stock.TmpSkuStock;
import org.bluedream.core.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @ClassName TmpSkuStockService
 * @Description TODO
 * @Author Administrator
 * @Data 2022/7/1 9:42
 * @Version
 */
@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class TmpSkuStockService extends OrgBaseServiceImpl<TmpSkuStockDao, TmpSkuStock> implements IService<TmpSkuStock> {
    private static final Logger logger = LoggerFactory.getLogger(TmpSkuStockService.class);

    @Resource
    private SkuStockService skuStockService;

    @Override
    public boolean save(TmpSkuStock entity) {
        //sumT 是不包含本条记录的商品实时库存数据
        TmpSkuStock sumT = skuStockService.getProductSumData(entity.getOrgCode() , entity.getWarCode() , entity.getCode());
        if (EmptyUtil.isEmpty(sumT)){
            sumT = new TmpSkuStock(entity.getOrgCode() , entity.getWarCode() , entity.getCode());
        }
        //typeLabel 进退货验证
        Integer typeLabel = entity.getBillTypeLabel();
        //t1 查询临时库存表中 单据号 、 商品是否存在
        QueryWrapper<TmpSkuStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id" , entity.getId());
        queryWrapper.eq("bill_type" , entity.getBillType());
        queryWrapper.eq("bill_type_label" , entity.getBillTypeLabel());
        queryWrapper.eq("bill_num" , entity.getBillNum());
        queryWrapper.eq("org_code" , entity.getOrgCode());
        queryWrapper.eq("war_code" , entity.getWarCode());
        TmpSkuStock t1 = baseMapper.getOneData(queryWrapper);

        entity.setIsNewRecord(false);
        if (EmptyUtil.isEmpty(t1)){
            entity.setIsNewRecord(true);
        }else {
            BigDecimal tQty = t1.getQty().add(entity.getQty());
            BigDecimal tTotal = t1.getTotal().add(entity.getTotal());
            BigDecimal tOprice = new BigDecimal("0");
            if (tTotal.compareTo(new BigDecimal("0")) != 0 || tQty.compareTo(new BigDecimal("0")) != 0){
                tOprice = tTotal.divide(tQty);
            }
            entity.setQty(tQty);
            entity.setOprice(tOprice);
            entity.setTotal(tTotal);
        }
        entity.setSumQty(sumT.getSumQty().add(entity.getQty().multiply(BigDecimal.valueOf(typeLabel))));
        entity.setSumTotal(sumT.getSumTotal().add(entity.getTotal().multiply(BigDecimal.valueOf(typeLabel))));
        return super.save(entity);
    }

    @Transactional(
            readOnly = false,
            isolation = Isolation.SERIALIZABLE,
            rollbackFor = Exception.class
    )
    public boolean delete(String billId , String billNum , String billType , String orgCode){
        QueryWrapper<TmpSkuStock> tmpSkuStockQueryWrapper = new QueryWrapper<>();
        tmpSkuStockQueryWrapper.eq("id" , billId);
        tmpSkuStockQueryWrapper.eq("bill_num" , billNum);
        tmpSkuStockQueryWrapper.eq("bill_type" , billType);
        tmpSkuStockQueryWrapper.eq("org_code" , orgCode);
        return super.remove(tmpSkuStockQueryWrapper);
    }
}